import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule

"""
获取一页的数据以后  需要进行翻页操作  用crawlspider如何实现 


"""
class Xs2Spider(CrawlSpider):
    name = 'xs2'
    allowed_domains = ['xbiquge.la']
    start_urls = ['https://www.xbiquge.la/fenlei/3_1.html']

    rules = (
        # 通过正则匹配url 不需要加上 分组匹配
        Rule(LinkExtractor(allow=r'https://www.xbiquge.la/\d*/\d*/'), callback='parse_item', follow=False),
        # 因为我们只需要完成翻页的效果 所以说不需要回调方法  因为要翻页所以要 follow=True
        Rule(LinkExtractor(restrict_xpaths=r'//a[@class="next"]'), follow=True),
    )
    def parse_item(self, response):
        name=response.xpath('//*[@id="info"]/h1/text()').get()
        print(name)
        item = {}
        #item['domain_id'] = response.xpath('//input[@id="sid"]/@value').get()
        #item['name'] = response.xpath('//div[@id="name"]').get()
        #item['description'] = response.xpath('//div[@id="description"]').get()
        return item
